<?php


namespace app\common\model;


class RoomChat extends BaseModel{

    protected $updateTime = false;

    public function user(){
        return $this->belongsTo('User','user_id','id')->field('id,nickname,avatar,level,education');
    }

    public function room(){
        return $this->belongsTo('Room','room_id','id');
    }

    public static function getList($roomId,$lastChatId,$page=1,$limit=''){
        if(empty($limit)){
            $limit = config('paginate.list_rows');
        }

        if($lastChatId){
            return self::with(['user','room'])->where('room_id',$roomId)->where('id','<=',$lastChatId)->page($page,$limit)->order('created_at','desc')->select();
        }else{
            return self::with(['user','room'])->where('room_id',$roomId)->page($page,$limit)->order('created_at','desc')->select();
        }
    }
}